package com.wskh.eduCenter.controller;


import com.wskh.commonUtil.JwtUtils;
import com.wskh.commonUtil.R;
import com.wskh.eduCenter.entity.UcenterMember;
import com.wskh.eduCenter.entity.vo.LoginVo;
import com.wskh.eduCenter.entity.vo.RegisterVo;
import com.wskh.eduCenter.service.UcenterMemberService;
import com.wskh.serviceBase.exception.XbException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wskh
 * @since 2021-10-20
 */
@Api(value = "登录注册管理")
@RestController
@RequestMapping("/eduCenter/member")
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE}) // 允许请求跨域
public class UcenterMemberController {

    @Autowired
    UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取用户登录信息")
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember member = ucenterMemberService.getById(memberId);
            if(member==null){
                throw new XbException(500,"获取登录信息失败");
            }
            return R.ok().data("item", member);
        }catch (Exception e){
            e.printStackTrace();
            throw new XbException(500,"获取登录信息失败");
        }
    }

    //根据用户id获取用户信息
    @PostMapping("getInfoUc/{id}")
    public UcenterMember getInfoUc(@PathVariable String id) {
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        UcenterMember memeber = new UcenterMember();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }

}

